/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package id3.UIs;

import id3.controllers.ID3Alg;
import id3.models.TreeNode;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 *
 * @author NguyenVan
 */
public class TestPanel extends JPanel implements ActionListener{
private final JTextField _txtFile;
    private final JFileChooser _file;
    private final JButton _btnChooseFile;
    private final JButton _btnTest;
    private final JTextArea _txtResult;
    public TestPanel() {
        setVisible(true);
        setLayout(new BorderLayout());
        _txtFile = new JTextField("", 30);
        _txtFile.setEditable(false);

        JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        jPanel.add(_txtFile);
        _file = new JFileChooser(System.getProperty("user.dir"));
        _btnChooseFile = new JButton("Chọn file");
        jPanel.add(_btnChooseFile);
        _btnTest = new JButton("Test");
        jPanel.add(_btnTest);
        add(jPanel, BorderLayout.NORTH);
        _btnChooseFile.addActionListener(this);
        _btnTest.addActionListener(this);
        
        JPanel panelResult=new JPanel(new GridLayout(1, 1, 10, 10));
        _txtResult=new JTextArea();
        _txtResult.setEditable(false);_txtResult.setBackground(Color.yellow);
        panelResult.add(_txtResult);
        _txtResult.setFont(new Font("Times New Roman", Font.BOLD, 27));
        add(panelResult,BorderLayout.CENTER);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == _btnChooseFile) {
            int input = _file.showOpenDialog(this);
            if (input == JFileChooser.APPROVE_OPTION) {
                _txtFile.setText(_file.getSelectedFile().getAbsolutePath());
            }
        }
        else if(e.getSource()==_btnTest){
            if(_txtFile.getText().length()==0){
                JOptionPane.showMessageDialog(this,"Can chon file truoc !", "Loi", JOptionPane.ERROR_MESSAGE);
                return;
            }
            if(TreeNode.instance==null){
                JOptionPane.showMessageDialog(this,"Can phai training truoc!", "Loi", JOptionPane.ERROR_MESSAGE);
                return;
            }
            try {
                _txtResult.setText(ID3Alg.test(_txtFile.getText(), TreeNode.instance));
                validate();
                repaint();
                JOptionPane.showMessageDialog(this,"Thanh Cong !", "Thanh Cong", JOptionPane.PLAIN_MESSAGE);
            } catch (Exception e1) {
                JOptionPane.showMessageDialog(this,"Da xay ra loi.Vui long thu lai !", "Loi", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
    
}
